package com.yxhl.zoume.core.coupon.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yxhl.zoume.CouponHomeData;
import com.yxhl.zoume.R;
import com.yxhl.zoume.account.event.LoginViewBackEvent;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.coupon.adapter.CouponPagerAdapter;
import com.yxhl.zoume.core.coupon.model.CouponStatusEnum;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by vhawk on 2016/11/3.
 */
public class CouponShowFragment extends BaseDataBindingFragment implements ViewPager.OnPageChangeListener {


    @BindView(R.id.view_pager_coupon)
    public ViewPager couponPager;
    private CouponHomeData couponHomeDataBinding;
    private CouponStatusEnum couponStatusEnum = CouponStatusEnum.COUPON_STATUS_ENUM_CURRENT;

    public static Fragment newInstance() {
        Fragment fragment = new CouponShowFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        couponPager.setAdapter(new CouponPagerAdapter(getChildFragmentManager()));
        couponPager.addOnPageChangeListener(this);
        onRxBusEvent();
    }

    private void onRxBusEvent() {
        getSubscription().add(toSubscription(LoginViewBackEvent.class, new Action1<LoginViewBackEvent>() {
            @Override
            public void call(LoginViewBackEvent loginViewBackEvent) {
                switch (loginViewBackEvent.getIsLogin()) {
                    case LOGIN:
                        getActivity().finish();
                        break;
                    case NOT_LOGIN:
                        getActivity().finish();
                        break;
                }
            }
        }));
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        couponHomeDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_coupon_home, container, false);
        return couponHomeDataBinding.getRoot();
    }

    @Override
    protected void setupToolBar() {

    }

    @OnClick(R.id.rl_coupon_status)
    public void couponStatusClick() {
        couponStatusEnum = couponStatusEnum.getNext();
        couponHomeDataBinding.setCouponStatus(couponStatusEnum);
        couponPager.setCurrentItem(getItemIndexByStatus(couponStatusEnum));
    }

    private int getItemIndexByStatus(CouponStatusEnum couponStatusEnum) {
        switch (couponStatusEnum) {
            case COUPON_STATUS_ENUM_CURRENT:
                return 0;
            case COUPON_STATUS_ENUM_HISTORY:
                return 1;
        }
        return 1;
    }

    private void setBottomStatusByPage(int position) {
        switch (position) {
            case 0:
                couponStatusEnum = CouponStatusEnum.COUPON_STATUS_ENUM_CURRENT;
                break;
            case 1:
                couponStatusEnum = CouponStatusEnum.COUPON_STATUS_ENUM_HISTORY;
                break;
        }
        couponHomeDataBinding.setCouponStatus(couponStatusEnum);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setBottomStatusByPage(position);
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
